(function() {

	var role = require("./role");
	var fs = require("fs");
	// var BusCon = require("../../core/bus/_con");
	// var GoodsCon = require("../../core/goods/_con");
	var FindPawCon = require("../../core/find_paw/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var emailSender = require("../../core/_include/email");
	var sms = require("../../core/_include/sms");
	var ResponObj = require("../_include/responObj");

	var mail_find_paw_user = fs.readFileSync(__dirname + "/mail.find_paw.user.html").toString();
	var mail_find_paw_bus = fs.readFileSync(__dirname + "/mail.find_paw.bus.html").toString();
	//PS:商家和用户的密码找回与重置的接口本来是可以共用一个，考虑到商家的安全性问题，可能会有不同的流程针对商家，所以还是分开来搞
	module.exports = {
		prefix: "/find_paw",
		get: {},
		post: {
			//邮箱找回，用户密码找回
			"/email/user": function() {
				var self = this;
				var body = this.request.body;
				var type = body.type || "user";
				var email = body.email;
				if (!(type && email)) {
					throwCusError("参数错误")
				}
				var find_paw = FindPawCon.buildByEmail(type, email);
				emailSender({
					to: email,
					title: "【点纳微站】用户密码重置",
					content: mail_find_paw_user.format({
						reset_code: find_paw._id
					})
				}, function(err) {
					if (err) {
						throwCusError("邮件发送失败")
					}
					self.body = ResponObj("string", "申诉成功，请查收邮箱")
				});
			},
			//邮箱找回，商家密码找回
			"/email/bus": function() {
				var self = this;
				var body = this.request.body;
				var type = body.type || "bus";
				var email = body.email;
				if (!(type && email)) {
					throwCusError("参数错误")
				}
				var find_paw = FindPawCon.buildByEmail(type, email);
				emailSender({
					to: email,
					title: "【点纳微站】商家密码重置",
					content: mail_find_paw_bus.format({
						reset_code: find_paw._id
					})
				}, function(err) {
					if (err) {
						throwCusError("邮件发送失败")
					}
					self.body = ResponObj("string", "申诉成功，请查收邮箱")
				});
			},
			"/mobile_phone/user": function() {
				var self = this;
				var body = this.request.body;
				var type = body.type || "user";
				var mobile_phone = body.mobile_phone;
				if (!(type && mobile_phone)) {
					throwCusError("参数错误")
				}
				var find_paw = FindPawCon.buildByPhone(type, mobile_phone);
				sms.send(mobile_phone, "您在点纳微站提交了“用户密码找回”的申请，您的重置验证码为：" + find_paw._id + "，请不要将链接与他人分享。")
				self.body = ResponObj("string", "申诉成功，请查收短信")
			},
			"/mobile_phone/bus": function() {
				var self = this;
				var body = this.request.body;
				var type = body.type || "bus";
				var mobile_phone = body.mobile_phone;
				if (!(type && mobile_phone)) {
					throwCusError("参数错误")
				}
				var find_paw = FindPawCon.buildByPhone(type, mobile_phone);
				sms.send(mobile_phone, "您在点纳微站提交了“商家密码找回”的申请，您的重置验证码为：" + find_paw._id + "，请不要将链接与他人分享。")
				self.body = ResponObj("string", "申诉成功，请查收短信")
			}
		},
		put: {
			"/by_find_paw_id/:find_paw_id": function() {
				var find_paw_id = this.params.find_paw_id;
				var body = this.request.body;
				var paw = body.paw;
				var confirm_paw = body.confirm_paw;
				if (!paw) {
					throwCusError("重置密码不可为空")
				}
				if (confirm_paw !== paw) {
					throwCusError("两次密码不一致")
				}
				var find_paw = FindPawCon.get(find_paw_id);
				if (!find_paw) {
					throwCusError("重置码无效")
				}
				var people = find_paw.resetPaw(paw);
				this.body = ResponObj("json", people);
			},
		}
	}
}());